package com.example.graduateinfo.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : 卷鑫菜
 * @date : 2023/1/4 17:59
 */

@WebFilter(filterName = "LoginFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest)request;
        String path = req.getRequestURI();
        String contextPath = req.getContextPath();
        if (path.equals("/graduateinfo/")) {
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.sendRedirect(contextPath + "/login");
            return;
        }
        chain.doFilter(request, response);
    }
}
